UI - TKinter -grid
Using Grid for the container. We have to specify row and column value for the control.
for example, label.grid(row = 0, column=0)
Here, row = 0 means first row, and column = 0 is the first
txt.grid(row=3,column=2)
Here, row = 3 means fourth row and column = 2 means third column
To add event to control, we use command property of control and assign reference of function to the command property
here, btnsum is button, command properties is assigned value add. add is a function
import tkinter as tk
def add():
n1 = int(t1.get("1.0","end-1c"))
n2 = int(t2.get("1.0","end-1c"))
ans = n1 + n2
lblanswer.config(text=ans)
def click():
name = txtname.get()
age = txtage.get()
str = name + "," + age
lblvalues.config(text=str)
if __name__ == "__main__":
container = tk.Tk() # creating container
container.title("User Interface") # giving title for the container
container.geometry("300x400") #specifying dimension of container (width, height)
container.resizable(width=5, height=5) #if zero resizing is not possible
lblname = tk.Label(container, text="Enter your name") #creating label
lblname.grid(row=0, column=0) #grid() treats the window like a table or spreadsheet with rows and columns.
txtname = tk.Entry(container, width=30) #creating textbox with single line
txtname.grid(row=0, column=1) #placing textbox in second column of first row
lblage = tk.Label(container, text="Enter your age")
lblage.grid(row=1, column=0) #label in first column of first row.
txtage = tk.Entry(container, width=30)
txtage.grid(row=1, column=1)
btn = tk.Button(container,text="Click",command=click)
btn.grid(row=2, column=0)
lblvalues = tk.Label(container, text="")
lblvalues.grid(row=3, column=0)
t1 = tk.Text(container,height=1, width=10)
t1.grid(row=4, column=0)
t2 = tk.Text(container, width=10,height=1)
t2.grid(row=4, column=1)
lblanswer = tk.Label(container, text="",height=1)
lblanswer.grid(row=5, column=0)
btnsum = tk.Button(container, width=10,command=add)
btnsum = tk.Button(container, width=10,command=add)
btnsum.grid(row=5, column=1)
container.mainloop()